package cn.coufran.springboot.starter.http.content;

import cn.coufran.springboot.starter.http.MultipartBody;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author Coufran
 * @version 2.0.0
 * @since 2.0.0
 */
public class TextContent extends HttpBodyContent<String> implements HttpRequestBodyContent, HttpResponseBodyContent {
    private String content;

    public TextContent(String content) {
        this.content = content;
    }

    public TextContent(String name, String content) {
        super(name);
        this.content = content;
    }

    @Override
    public String content() {
        return content;
    }

    @Override
    public void accept(MultipartBody.Builder multipartBodyBuilder) {
        multipartBodyBuilder.addPart(name(), content());
    }

    @Override
    public InputStream getInputStream() {
        return new ByteArrayInputStream(asBytes());
    }

    @Override
    public byte[] asBytes() {
        return content().getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public String asString() {
        return content();
    }

    @Override
    public String toString() {
        return "TextContent{" +
                "content='" + content + '\'' +
                "} " + super.toString();
    }
}
